//
//  CycBLEManager.h
//  Cycling
//
//  Created by cjg.mini on 2023/3/9.
//

#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>

// 蓝牙服务UUID
#define PERIPHERAL_SERVICE_UUID @"FF00"

// DATAOUT特征值, notify,用于发送数据给controller
#define PERIPHERAL_DATAOUT_CHAR_UUID @"FF01"

// DATAIN特征值， write（write with response）,用于接收来自controller的数据
#define PERIPHERAL_DATAIN_CHAR_UUID @"FF02"

// Data fc, notify , 用于通知controller目前的流控状态和最大传输单元（mtu）
#define PERIPHERAL_DATAFC_CHAR_UUID @"FF03"

extern NSString * const CYCBLEManagerErrorDomain;

/// 状态
typedef NS_ENUM(NSInteger, CYCBLEManagerState) {
    CYCBLEManagerStateUnavailable, /// 不可用
    CYCBLEManagerStateReady,       /// 准备好了
    CYCBLEManagerStateIdle,        /// 闲置
    CYCBLEManagerStateScanning,    /// 搜索中
    CYCBLEManagerStateConnecting,  /// 连接中
    CYCBLEManagerStateConnected,   /// 已连接
    CYCBLEManagerStateSubscribed,  /// 订阅
};

/// 错误
typedef NS_ENUM(NSInteger, CYCBLEManagerErrorCode) {
    CYCBLEManagerErrorCodeBluetoothPoweredOff = 0,
    CYCBLEManagerErrorCodeBluetoothUnavailable = 1,
    CYCBLEManagerErrorCodeConnectionFailed = 2,
    CYCBLEManagerErrorCodeAutoDisconnected = 3,
    CYCBLEManagerErrorCodeCannotSubscribe = 4,
    CYCBLEManagerErrorCodeWrongState = 5,
    CYCBLEManagerErrorCodeConnectionTimeout = 6,
};

typedef void(^CYCBLEManagerScanSuccessBlock)(NSArray<CBPeripheral *> *peripherals);     /// 搜索成功
typedef void(^CYCBLEManagerScanFailureBlock)(NSError *error);                           /// 搜索失败
typedef void(^CYCBLEManagerConnectionSuccessBlock)(CBPeripheral *peripheral);           /// 连接成功
typedef void(^CYCBLEManagerConnectionFailureBlock)(NSError *error);                     /// 连接失败
typedef void(^CYCBLEManagerStateChangeBlock)(CYCBLEManagerState state);                 /// 状态改变
typedef void(^CYCBLEManagerReceiveDataBlock)(NSData *data);                             /// 收到数据

@protocol CYCBLEManagerDelegate;

NS_ASSUME_NONNULL_BEGIN

@interface CycBLEManager : NSObject

@property (nonatomic, strong, readonly) NSMutableArray<CBPeripheral *> *discoveredPeripherals;  //!< 所有设备
@property (nonatomic, assign, readonly) CYCBLEManagerState state;                               //!< 蓝牙状态
@property (nonatomic, assign) BOOL connected;                                                   //!< 连接与否
@property (nonatomic, copy) CYCBLEManagerReceiveDataBlock receiveDataoutBlock;                  //!< 收到data out数据回调
@property (nonatomic, copy) CYCBLEManagerReceiveDataBlock receiveDatafcBlock;                   //!< 收到data fc数据回调
@property (nonatomic, strong) CBPeripheral *connectedPeripheral;

+ (instancetype)sharedManager;

- (void)startScanWithSuccess:(CYCBLEManagerScanSuccessBlock)success failure:(CYCBLEManagerScanFailureBlock)failure;
- (void)stopScan;
- (void)connectPeripheral:(CBPeripheral *)peripheral success:(CYCBLEManagerConnectionSuccessBlock)success failure:(CYCBLEManagerConnectionFailureBlock)failure;
- (void)cancelConnection:(CBPeripheral *)peripheral;
- (void)disconnect;
- (void)sendData:(NSData *)data;
- (void)addStateChangeBlock:(CYCBLEManagerStateChangeBlock)block forKey:(NSString *)key;
- (void)removeStateChangeBlockForKey:(NSString *)key;


@end

NS_ASSUME_NONNULL_END
